package io.renren.modules.bus.controller;

import java.util.*;
import java.math.BigDecimal;

import io.renren.modules.bus.vo.CostRecordVO;
import io.renren.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.renren.modules.bus.entity.CostRecordEntity;
import io.renren.modules.bus.service.CostRecordService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 消费记录
 * @Author lililiang
 * @Date 2019-02-23 15:07:23
 */
@RestController
@Api(tags = "消费记录")
@RequestMapping("/bus/costrecord")
public class CostRecordController extends AbstractController{
    @Autowired
    private CostRecordService costRecordService;

    /**
     * 分页查询
     */
    @ApiOperation(value = "分页查询", response = CostRecordEntity.class)
    @GetMapping("/list")
    @RequiresPermissions("bus:costrecord:list")
    public R list(
            @ApiParam(value = "id", required = false) @RequestParam(required = false) Integer id,
            @ApiParam(value = "物品编码", required = false) @RequestParam(required = false) String goodsCode,
            @ApiParam(value = "物品名称", required = false) @RequestParam(required = false) String goodsName,
            @ApiParam(value = "会员名称", required = false) @RequestParam(required = false) String memberName,
            @ApiParam(value = "创建人名称", required = false) @RequestParam(required = false) String authorName,
            @ApiParam(value = "会员Id", required = false) @RequestParam(required = false) Integer memberId,
            @ApiParam(value = "原价", required = false) @RequestParam(required = false) BigDecimal originalPrice,
            @ApiParam(value = "折扣比例", required = false) @RequestParam(required = false) Integer discount,
            @ApiParam(value = "使用积分", required = false) @RequestParam(required = false) Integer usePoint,
            @ApiParam(value = "积分兑换金额", required = false) @RequestParam(required = false) BigDecimal pointMoney,
            @ApiParam(value = "实付价", required = false) @RequestParam(required = false) BigDecimal actuallyPaid,
            @ApiParam(value = "付款方式", required = false) @RequestParam(required = false) String payment,
            @ApiParam(value = "变化积分", required = false) @RequestParam(required = false) Integer changePoint,
            @ApiParam(value = "当前积分（本次消费后的当前积分）", required = false) @RequestParam(required = false) Integer currentPoint,
            @ApiParam(value = "店铺Id", required = false) @RequestParam(required = false) Integer storeId,
            @ApiParam(value = "备注", required = false) @RequestParam(required = false) String remark,
            @ApiParam(value = "创建人", required = false) @RequestParam(required = false) Integer author,
            @ApiParam(value = "创建时间", required = false) @RequestParam(required = false) Date createTime,
            @ApiParam(value = "状态，1有效，0无效，-1删除", required = false) @RequestParam(required = false) Integer status
        ){
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("goodsCode", goodsCode);
        params.put("goodsName", goodsName);
        params.put("memberName", memberName);
        params.put("authorName", authorName);
        params.put("memberId", memberId);
        params.put("originalPrice", originalPrice);
        params.put("discount", discount);
        params.put("usePoint", usePoint);
        params.put("pointMoney", pointMoney);
        params.put("actuallyPaid", actuallyPaid);
        params.put("payment", payment);
        params.put("changePoint", changePoint);
        params.put("currentPoint", currentPoint);
        params.put("storeId", storeId);
        params.put("remark", remark);
        params.put("author", author);
        params.put("createTime", createTime);
        params.put("status", status);
        PageUtils page = costRecordService.list4Page(params);
        return R.ok().put("page", page);
    }

    /**
     * 列表查询
     */
    @ApiOperation(value = "列表查询", response = CostRecordEntity.class)
    @GetMapping("/listByCondition")
    @RequiresPermissions("bus:costrecord:list")
    public R listByCondition(
            @ApiParam(value = "id", required = false) @RequestParam(required = false) Integer id,
                @ApiParam(value = "物品编码", required = false) @RequestParam(required = false) String goodsCode,
                @ApiParam(value = "物品名称", required = false) @RequestParam(required = false) String goodsName,
                @ApiParam(value = "会员Id", required = false) @RequestParam(required = false) Integer memberId,
                @ApiParam(value = "原价", required = false) @RequestParam(required = false) BigDecimal originalPrice,
                @ApiParam(value = "折扣比例", required = false) @RequestParam(required = false) Integer discount,
                @ApiParam(value = "使用积分", required = false) @RequestParam(required = false) Integer usePoint,
                @ApiParam(value = "积分兑换金额", required = false) @RequestParam(required = false) BigDecimal pointMoney,
                @ApiParam(value = "实付价", required = false) @RequestParam(required = false) BigDecimal actuallyPaid,
                @ApiParam(value = "付款方式", required = false) @RequestParam(required = false) String payment,
                @ApiParam(value = "变化积分", required = false) @RequestParam(required = false) Integer changePoint,
                @ApiParam(value = "当前积分（本次消费后的当前积分）", required = false) @RequestParam(required = false) Integer currentPoint,
                @ApiParam(value = "店铺Id", required = false) @RequestParam(required = false) Integer storeId,
                @ApiParam(value = "备注", required = false) @RequestParam(required = false) String remark,
                @ApiParam(value = "创建人", required = false) @RequestParam(required = false) Integer author,
                @ApiParam(value = "创建时间", required = false) @RequestParam(required = false) Date createTime,
                @ApiParam(value = "状态，1有效，0无效，-1删除", required = false) @RequestParam(required = false) Integer status
        ){
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("goodsCode", goodsCode);
        params.put("goodsName", goodsName);
        params.put("memberId", memberId);
        params.put("originalPrice", originalPrice);
        params.put("discount", discount);
        params.put("usePoint", usePoint);
        params.put("pointMoney", pointMoney);
        params.put("actuallyPaid", actuallyPaid);
        params.put("payment", payment);
        params.put("changePoint", changePoint);
        params.put("currentPoint", currentPoint);
        params.put("storeId", storeId);
        params.put("remark", remark);
        params.put("author", author);
        params.put("createTime", createTime);
        params.put("status", status);
        List<CostRecordEntity> list = costRecordService.listByCondition(params);
        return R.ok().put("list", list);
    }

    /**
     * 查询详情
     */
    @ApiOperation(value = "查询详情", response = CostRecordEntity.class)
    @GetMapping("/info/{id}")
    @RequiresPermissions("bus:costrecord:info")
    public R info(@PathVariable("id") Integer id){
        CostRecordVO costRecord = costRecordService.queryOne(id);
        return R.ok().put("costRecord", costRecord);
    }

    /**
     * 新增保存
     */
    @ApiOperation(value = "新增保存", response = R.class)
    @PostMapping("/save")
    @RequiresPermissions("bus:costrecord:save")
    public R save(@RequestBody CostRecordVO costRecordVO){
        costRecordVO.setAuthor(this.getUserId().intValue());
        costRecordService.save(costRecordVO, 1);
        return R.ok();
    }

    /**
     * 修改保存
     */
    @ApiOperation(value = "修改保存", response = R.class)
    @PostMapping("/update")
    @RequiresPermissions("bus:costrecord:update")
    public R update(@RequestBody CostRecordVO costRecordVO){
		costRecordService.save(costRecordVO, 2);
        return R.ok();
    }

    /**
     * 批量逻辑删除
     */
    @ApiOperation(value = "批量逻辑删除", response = R.class)
    @PostMapping("/delete")
    @RequiresPermissions("bus:costrecord:delete")
    public R delete(@RequestBody Integer[] ids){
		costRecordService.deleteLogic(Arrays.asList(ids));
        return R.ok();
    }

}
